<template>
    <div class="container-box">
        <div style="height: 98px;"></div>
        <!-- serch -->
        <div class="bgfffs filter_box" :style="contentStyleObj">
            <page-filter :className="className" lableWidth="70px" ref="from" :query.sync="filterInfo.query"
                :filter-list="filterInfo.list" :list-type-info="listTypeInfo" @handleClick="handleClick"
                @handleEvent="handleEvent" @upenter="upenter">
            </page-filter>
        </div>

        <div class="bgfff" ref="tableWrapper">
            <div style="padding: 10px 10px">
                <page-table :checkBox="false" ref="table" :tabIndex="true" :dataText="dataText" v-loading="loading"
                    :init-curpage="tableInfo.initCurpage" :data.sync="tableInfo.data" :pageParm="pageParm"
                    showRow="invoice" :query="filterInfo.query" :field-list="tableInfo.fieldList"
                    :list-type-info="listTypeInfo" :showSummary="showSummary" :getSummaries="getSummaries"
                    :handle="tableInfo.handle" @handleClick="handleClick" @handleEvent="handleEvent">
                </page-table>
            </div>
        </div>
    </div>
</template>

<script>

import PageDialog from "@/components/PageDialog";
import PageFilter from "@/components/PageFilter/index";
import PageTable from "@/components/PageTable/index";
import { formatter } from "@/utils/index";
import {
    getPaymentList,// 列表
    getPaymentParam,// 发票基础参数
    confirmPay,// 确认提现
} from '@/api/bill'
export default {
    name: "paymentPage",
    components: {
        PageFilter,
        PageDialog,
        PageTable
    },
    data () {
        return {
            id: 0,
            fullYear: new Date().getFullYear(),
            month: new Date().getMonth() + 1,
            loading: false,
            is_modify_tax: 0,
            billLading: '',
            addId: [],
            dataText: "",
            showSummary: false,
            pay_amount: 0,//付款金额
            amount: 0,//运费
            other_amount: 0,//运杂费
            reward_amount: 0,//奖励金
            total: 0,//累计付款
            settlement_time: "",
            contentStyleObj: {
                height: "100%"
            },
            refresh: false,

            className: "forms",
            // 相关列表
            listTypeInfo: {
                user_id: [],//付款用户
                status: [], //提现状态

            },

            // 弹窗相关
            dialogInfo: {
                width: "900px",
                title: {

                },
                visible: false,
                type: "",
                btLoading: false,
                btList: [
                    {
                        label: "确定",
                        event: "save",
                        show: true
                    }
                ]
            },
            // 过滤相关配置
            filterInfo: {
                query: {
                    page: 1,
                    limit: 20,
                    user_id: "",
                    status: "",
                    start_time: "",
                    end_time: ""
                },
                list: [
                    {
                        type: "select",
                        label: "付款用户",
                        value: "user_id",
                        list: "user_id",
                        key: "付款用户"
                    },
                    {
                        type: "timedate",
                        dateType: "date",
                        format: "yyyy-MM-dd",
                        label: "申请开始时间",
                        value: "start_time",
                        clearable: false,
                        key: "申请时间"
                    },
                    {
                        type: "timedate",
                        dateType: "date",
                        format: "yyyy-MM-dd",
                        label: "申请结束时间",
                        value: "end_time",
                        clearable: false,
                        key: "-"
                    },

                    {
                        type: "select",
                        label: "付款状态",
                        value: "status",
                        list: "status",
                        key: "付款状态"
                    },
                    {
                        type: "button",
                        label: "搜索",
                        btType: "primary",
                        event: "search",
                        show: true
                    },
                    {
                        type: "button",
                        label: "重置",
                        event: "reset",
                        show: true
                    }
                ]
            },
            // 分页参数
            pageParm: {
                tableHeight: "52vh", // 表格最大高度
                total: 0, // 总条数
                loading: true, // 加载动画
                pagesizes: [20, 50, 100], // 分页数量列表
                query: {
                    // 查询条件
                    page: 1, // 当前页
                    limit: 20 // 每页条数
                }
            },
            // 表格相关
            tableInfo: {
                refresh: 1,
                initCurpage: 1,
                data: [],
                fieldList: [
                    {
                        label: "申请时间",
                        value: "add_time",
                        width: "150px",
                    },
                    { label: "状态", value: "status_text", width: "100px" },
                    { label: "付款金额", value: "pay_amount", width: "120px" },
                    { label: "申请人", value: "real_name", width: "100px" },
                    { label: "联系方式", value: "phone", width: "120px" },
                    { label: "收款银行", value: "bank_name", width: "150px" },
                    { label: "银行账号", value: "bank_account" },
                    { label: "累计付款", value: "total", width: "120px" },
                ],
                handle: {
                    label: "操作",
                    width: "200px",
                    btList: [
                        {
                            label: "已付款",
                            type: "text",
                            //icon: "el-icon-browse",
                            //event: "uploadInvoice",
                            show: true,
                            ifRender: row => {
                                if (row.status == 1) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        },
                        {
                            label: "确认",
                            type: "text",
                            icon: "el-icon-browse",
                            event: "confirm",
                            show: true,
                            ifRender: row => {
                                if (row.status == 2) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        },
                    ]
                },
            }
        };
    },
    created () {

        //this.getonemonth();
        this.init({ ...this.pageParm.query, ...this.filterInfo.query });
        this.getParamList();
        // this.getHeight();
        this.fetTableHeight()
        this.upenter();
        //window.addEventListener("resize", this.getHeight);
    },
    //enter事件 与点击查询按钮功能一致
    updated () {
        var that = this;
    },
    activated () {
        this.init(this.filterInfo.query);
        this.getParamList();
        this.upenter();
        this.refresh = false;
    },
    deactivated () {
        this.refresh = true;
    },
    filters: {
        formatter (time) {
            let date = new Date(time),
                text = "";
            if (time !== "") return (text = formatter(date, "yyyy-MM"));
            return text;
        }
    },
    computed: {
    },

    methods: {
        //合计
        getSummaries (param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 2) {
                    sums[index] = "合计：";
                    return;
                }
                if (index === 3) {
                    sums[index] = this.pay_amount;
                    return;
                }
                if (index === 4) {
                    sums[index] = this.amount;
                    return;
                }

                if (index === 8) {
                    sums[index] = this.total;
                    return;
                }

            });
            this.$nextTick(() => {
                this.$refs.table.$refs.table.doLayout();
                this.$refs.table.$refs.table.bodyWrapper.scrollLeft = 0;
            });
            return sums;
        },
        handleClose (tag) {
            this.relationMixedCost.splice(this.relationMixedCost.indexOf(tag), 1);
        },
        unique (arr) {
            const res = new Map();
            return arr.filter(a => !res.has(a) && res.set(a, 1));
        },
        arrayAminusB (arrA, arrB) {
            return arrA.filter(v => !arrB.includes(v));
        },
        //enter 事件防止全局触发
        upenter () {
            var that = this;
            document.onkeydown = e => {
                //13表示回车键，baseURI是当前页面的地址，为了更严谨，也可以加别的，可以打印e看一下
                if (e.keyCode === 13 && e.target.baseURI.match("receivableVerification") && !that.showInvoice && !that.showReconciliation && !that.showDestroy && !that.dialogInfo.visible) {
                    console.log("回车事件", that.dialogInfo.visible);
                    //回车后执行搜索方法
                    that.filterInfo.query.page = 1;
                    that.pageParm.query.page = 1;
                    that.init(that.filterInfo.query);
                }
            };
        },
        openWind () {
        },
        getHeight () {
            if (this.contentStyleObj.height == "100%") {
                var height =
                    window.innerHeight - parseInt(this.contentStyleObj.height) - 120;
            } else {
                var height =
                    window.innerHeight - parseInt(this.contentStyleObj.height) - 146;
            }
            this.pageParm.tableHeight = height + "px";
        },
        // 重置table高度
        resetHeight () {
            return new Promise((resolve, reject) => {
                this.tableH = 0
                resolve()
            })
        },
        // 设置table高度
        fetTableHeight () {
            this.resetHeight().then(res => {
                this.pageParm.tableHeight = this.$refs.tableWrapper.getBoundingClientRect().height - 64
                // 设置最小高度，确保表格可以正常显示
                // this.tableH = Math.max(containerHeight - 10, 200); // 最小300px
            })
        },
        //获取本月月初 月末
        // getonemonth() {
        //   var nowDate = new Date();
        //   var fullYear = nowDate.getFullYear();
        //   var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11，代表1-12月
        //   var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天

        //   this.settlement_time =  nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1);
        //   this.filterInfo.query.settlement_time =  nowDate.getFullYear();
        // },
        //格式化年月日
        getFullDate (targetDate) {
            var D, y, m, d;
            if (targetDate) {
                D = new Date(targetDate);
                y = D.getFullYear();
                m = D.getMonth() + 1;
                d = D.getDate();
            } else {
                y = fullYear;
                m = month;
                d = date;
            }
            m = m > 9 ? m : "0" + m;
            d = d > 9 ? d : "0" + d;
            return y + "-" + m + "-" + d;
        },
        init (data) {
            this.loading = true;
            let formData = new FormData();
            formData.append('user_id', data.user_id);
            formData.append('start_time', data.start_time);
            formData.append('end_time', data.end_time);
            formData.append('status', data.status);
            // formData.append('page',data.page);
            // formData.append('limit',data.limit);
            getPaymentList(formData).then(res => {
                this.showSummary = true;
                this.tableInfo.data = [];
                this.tableInfo.data = res.data.data;
                if (this.tableInfo.data.length === 0) {
                    this.dataText = "暂无数据";
                    this.pay_amount = 0.00;
                    this.amount = 0.00;
                    this.other_amount = 0.00;
                    this.reward_amount = 0.00;
                    this.total = 0.00;
                }
                this.pay_amount = res.data.other.pay_amount;
                this.amount = res.data.other.amount;
                this.other_amount = res.data.other.other_amount;
                this.reward_amount = res.data.other.reward_amount;
                this.total = res.data.other.total;
                this.pageParm.total = res.data.meta.total;
                this.loading = false;
            });

        },


        getCurrentDate () {
            const today = new Date();
            const year = today.getFullYear();
            const month = String(today.getMonth() + 1).padStart(2, '0');
            const day = String(today.getDate()).padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
        getCurrentMonth () {
            const today = new Date();
            const year = today.getFullYear();
            const month = String(today.getMonth() + 1).padStart(2, '0');
            return `${year}-${month}`;
        },

        getParamList () {
            getPaymentParam().then(res => {
                let {
                    user_id,//付款用户
                    status, //提现状态

                } = res.data;

                user_id = user_id.map(v => {
                    return {
                        key: v.value,
                        value: v.name
                    };
                });

                status = status.map(v => {
                    return {
                        key: v.value,
                        value: v.name
                    };
                });
                this.listTypeInfo.user_id = user_id;
                this.listTypeInfo.status = status;
            });

        },

        handleClick (event, data) {
            console.log(event, data);
            console.log('当前事件---', event)
            switch (event) {
                case "confirm":
                    this.$confirm("确认通过司机提现吗？", "确认提现", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    })
                        .then(() => {
                            let formData = new FormData();
                            formData.append('id', data.id)
                            formData.append('status', 1)

                            confirmPay(formData)
                                .then(res => {
                                    if (res.status == 200) {
                                        this.$message({
                                            message: res.msg,
                                            type: "success"
                                        });
                                        this.init(this.filterInfo.query);
                                    }
                                })
                                .catch(() => { });
                        })
                        .catch(() => {
                            this.$message({
                                type: "info",
                                message: "已取消"
                            });
                        });
                    break;
                case "search":
                    this.pageParm.query.page = 1;
                    this.filterInfo.query.page = 1;
                    this.init(this.filterInfo.query);
                    break;
                case "reset":
                    for (let i in this.filterInfo.query) {
                        this.filterInfo.query[i] = "";
                    }
                    this.filterInfo.query.page = 1;
                    this.filterInfo.query.limit = 20;
                    this.pageParm.query.page = 1;
                    this.pageParm.query.limit = 20;
                    let datas = {};
                    datas = Object.assign({}, this.pageParm.query, this.filterInfo.query);
                    this.init(datas);
                    break;
                case "save":

                    break;
                case "close":
                    this.dialogInfo.btLoading = false;
                    this.dialogInfo.visible = false;
                    this.is_modify_tax = 0
                    this.modifyTaxRadioForm.tax_ratio = ''
                    this.modifyTaxRadioForm.modify_freight = ''
                    break;
            }
        },
        handleEvent (event, data, label) {
            let datas = {};
            switch (event) {
                case "tableCheck":
                    this.addId = [];
                    if (this.addId.length > 0) this.isBill = false
                    else this.isBill = true
                    data.forEach(v => {
                        this.addId.push(v.id);
                    });
                    break;
                case "rowClick":
                    break;
                case "search":
                    console.log('333333')
                    this.pageParm.query.page = 1;
                    this.filterInfo.query.page = 1;
                    this.init(this.filterInfo.query);
                    break;
                // 分页改变当前页
                case "curPage":
                    if (!data) return;
                    this.pageParm.query.page = data;
                    this.filterInfo.query.page = data;
                    datas = Object.assign({}, this.pageParm.query, this.filterInfo.query);
                    this.init(datas);
                    break;
                // 分页列表
                case "pageSize":
                    if (!data) return;
                    this.pageParm.query.limit = data;
                    this.filterInfo.query.limit = data;
                    this.pageParm.query.page = 1;
                    this.filterInfo.query.page = 1;
                    datas = Object.assign({}, this.pageParm.query, this.filterInfo.query);
                    this.init(datas);
                    break;
                default:
                    break;
            }
        },
    },
    destroyed () {
        window.removeEventListener("resize", this.getHeight);
    }
};
</script>
<style lang="scss">
.taskList_box_title {
    padding-bottom: 20px;
    line-height: 40px;
}

.taskList_box {
    width: 600px;
    height: 500px;
    overflow-y: auto;
    border: 1px solid #ddd;
    margin-right: -10px;
    border: 1px solid #ddd;
    line-height: 47px;
    width: 623px;

    .el-col {
        height: 60px;
        line-height: 40px;
        padding: 10px 0;

        .el-input-number {
            width: 140px;
        }
    }
}

/* 去掉input尾部上下小箭头 start */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
    -moz-appearance: none !important;
    -o-appearance: none !important;
    -ms-appearance: none !important;
    appearance: none !important;
    margin: 0;
}

input[type="number"] {
    -webkit-appearance: textfield;
    -moz-appearance: textfield;
    -o-appearance: textfield;
    -ms-appearance: textfield;
    appearance: textfield;
}

/* 去掉input尾部上下小箭头 end */
</style>
<style lang="scss" scoped>
.icon-box {
    display: inline-block;
    font-size: 12px;
    margin-left: 10px;

    span {
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        text-align: center;
        line-height: 20px;
        color: #fff;
    }
}


.filtrate {
    width: 25px;
    height: 25px;
    text-align: center;
    line-height: 25px;
    background-color: #409eff;
    color: #ffffff;
    box-shadow: -1px 1px 4px 0 #f6900f;
    position: absolute;
    right: 20px;
    top: 20px;
    z-index: 2;
}

.filtrate:hover {
    cursor: pointer;
}

::v-deep .receivable {
    margin-left: 33px;
}

::v-deep .moneycss {
    width: 120px;
}

.editicon:hover {
    cursor: pointer;
}

.imgggcss {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.excelcss:hover {
    cursor: pointer;
}

::v-deep .excelcss>.el-image__inner {
    vertical-align: middle;
}

.salecss {
    white-space: nowrap;
    /*强制span不换行*/
    display: inline-block;
    /*将span当做块级元素对待*/
    width: 60px;
    /*限制宽度*/
    overflow: hidden;
    /*超出宽度部分隐藏*/
    text-overflow: ellipsis;
    /*超出部分以点号代替*/
    line-height: 0.9;
    /*数字与之前的文字对齐*/
}

.salecss:hover {
    cursor: pointer;
}

.recordTable {
    display: flex;
    flex-direction: column;
}

.concatcss {
    padding-top: 10px;
    padding-left: 10px;
    margin-bottom: 20px;
    position: relative;
    margin-top: 10px;
}

.concatcss_line {
    width: 360px;
    border: 1px dashed #c8b9b9;
}

::v-deep.el-form-item {
    margin-bottom: 0px;
}

::v-deep.concatcss>.el-form-item>.el-form-item__label {
    font-size: 12px !important;
}

::v-deep.concatcss>.el-form-item>.el-form-item__content>.el-input>.el-input__inner {
    font-size: 12px !important;
}

::v-deep.concatcss>.el-form-item>.el-form-item__content>.el-input>.el-input__inner,
.concatcss>.el-form-item>.el-form-item__content {
    height: 30px !important;
    line-height: 30px;
}

::v-deep.concatcss>.el-form-item>.el-form-item__content>.el-select>.el-input>.el-input__inner,
.el-form-item__content>.filter-input>.el-input__inner {
    width: 200px;
    height: 30px;
    font-size: 12px !important;
}

.moredestroyCss {
    ::v-deep.el-form-item {
        margin-bottom: 20px !important;
    }

    ::v-deep .el-input {
        width: 240px !important;
    }

    ::v-deep .el-input__inner {
        width: 240px !important;
    }

    .item {
        width: 330px;
        margin-top: 15px;

        &-title {
            font-size: 13px;
            color: #9BA2AA;
            padding-left: 15px;
            padding-bottom: 5px;
        }

        &-main {
            padding: 10px 15px;
            box-sizing: border-box;
            border-radius: 4px;
            border: 1px dashed #000;

            &-line {
                display: flex;
                align-items: center;
                height: 40px;
                line-height: 40px;
            }

            &-title {
                width: 80px;
                color: #606266;
                font-size: 12px;
                font-weight: 700;

            }

            &-box {
                font-size: 12px;
                color: #606266;

                ::v-deep .el-input__inner {
                    width: 200px !important;
                }

                ::v-deep .el-input {
                    width: 200px !important;
                }
            }
        }
    }
}

.bgfff {
    background: #fff;
    flex: 1;
}

.bgfffs {
    background: #fff;
}

.tableHeight {
    flex: 1;
    box-sizing: border-box;
}

.filter_box {
    position: relative;
    // height: 64px;
    overflow: hidden;
    padding-right: 90px;

    .open {
        position: absolute;
        top: 10px;
        right: 20px;
        color: #409EFF;
        cursor: pointer;
    }
}

.app-main {
    padding-top: 90px !important;
}

.container-box {
    background: #f5f7fa;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    position: relative;
}

::v-deep .pagination-container {
    margin-top: 0 !important;
}

.addicon {
    color: #409eff;
    font-size: 20px;
    vertical-align: middle;
}

.addicon:hover {
    cursor: pointer;
}
</style>